﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Application.Share;
using Windows.FileStorage.Domain;
using Windows.FileStorage.Infrastructure;
using Windows.FileStorage.Infrastructure.EFCore;
using Windows.Infrastructure.Share.Security;
using Windows.Infrastructure.Share.Tools;

namespace Windows.FileStorage.Application
{
    public class AttachmentService: IAttachmentService
    {
        private readonly IMapper _mapper;
        //private readonly ICache _cache;
        private readonly FileStorageDbContext _db;
        public AttachmentService(IMapper mapper, FileStorageDbContext db)
        {
            _mapper = mapper;
            _db = db;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<AttachmentResponse> UploadImage(AttachmentRequest request)
        {
            try
            {
                if (request.File == null)
                {
                    throw new ApiException("文件接收异常");
                }
                //图片文件校验 
                switch (request.File.ContentType)
                {
                    case "image/jpeg":
                        break;
                    case "image/png":
                        break;
                    default:
                        throw new ApiException("文件格式异常");
                }
                string fileExtend = Path.GetExtension(request.File.FileName);
                string filePath = $"/Image/{DateTime.Now:yyyyMMdd}/";
                string fileName = $"{Guid.NewGuid()}{fileExtend}";

                Stream stream = request.File.OpenReadStream();
                var setting = ConfigurationUtils.GetSection<FtpSetting>("FtpSetting");
                FtpHelper ftpHelper = new(setting);
                ftpHelper.UploadStream(stream, filePath + fileName);
                Attachment attachment = new()
                {
                    OwnerId = request.OwnerId,
                    FileName = fileName,
                    SavePath = filePath + fileName,
                    FileExtend = fileExtend.Trim('.'),
                    FileSize = request.File.Length,
                    Type = 0
                };
                await _db.Attachment.AddAsync(attachment);
                await _db.SaveChangesAsync();
                AttachmentResponse response = new()
                {
                    AttachmentId = attachment.Id,
                    AttachmentPath = "http://localhost:8888/" + filePath + fileName,
                };
                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 上传员工照片
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<AttachmentResponse> UploadEmployeePhoto(AttachmentRequest request)
        {
            try
            {
                if (request.File == null)
                {
                    throw new ApiException("文件接收异常");
                }
                //图片文件校验 
                switch (request.File.ContentType)
                {
                    case "image/jpeg":
                        break;
                    case "image/png":
                        break;
                    default:
                        throw new ApiException("文件格式异常");
                }
                string fileExtend = Path.GetExtension(request.File.FileName);
                string filePath = $"/EmployeeImage/";
                string fileName = $"{Guid.NewGuid()}{fileExtend}";

                Stream stream = request.File.OpenReadStream();
                var setting = ConfigurationUtils.GetSection<FtpSetting>("FtpSetting");
                FtpHelper ftpHelper = new(setting);
                ftpHelper.UploadStream(stream, filePath + fileName);
                Attachment attachment = new()
                {
                    OwnerId = request.OwnerId,
                    FileName = fileName,
                    SavePath = filePath + fileName,
                    FileExtend = fileExtend.Trim('.'),
                    FileSize = request.File.Length,
                    Type = 0
                };
                await _db.Attachment.AddAsync(attachment);
                await _db.SaveChangesAsync();
                AttachmentResponse response = new()
                {
                    AttachmentId = attachment.Id,
                    AttachmentPath = "http://localhost:8888/" + filePath + fileName,
                };
                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task<string> GetAttachment(int ownerId)
        {
            var model = await _db.Attachment.Where(x => x.OwnerId == ownerId).FirstOrDefaultAsync();
            if (model == null)
                return null;
            return "http://localhost:8888/"+ model.SavePath;
        }
        /// <summary>
        /// 获取系统所有图标
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> GetAllSystemIcons()
        {
            var setting = ConfigurationUtils.GetSection<FtpSetting>("FtpSetting");
            FtpHelper ftpHelper = new(setting);
            return ftpHelper.GetFiles("SystemIcon").Select(s => "http://localhost:8888/" + s).ToList();
        }
    }
}
